


package yf.mall.controller.app;

import com.yf.request.SearchParam;

import java.io.IOException;
import java.util.List;

import com.yf.response.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.utils.StringUtil;
import yf.mall.entity.AppCategoryEntity;
import yf.mall.vo.AppCategoryVo;
import yf.mall.service.app.AppCategoryService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.stream.Collectors;

import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/app/category")
public class AppCategoryController {
    @Autowired
    private AppCategoryService appCategoryService;

    @RequestMapping("/list")
    @RequiresPermissions("app:category:list")
    public R list(@RequestBody SearchParam<AppCategoryVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<AppCategoryVo> list = appCategoryService.findVo(params.getEntity());
        return R.ok(new PageInfo(list));
    }

    @RequestMapping("/down")
    @RequiresPermissions("app:category:list")
    public void down(@RequestBody SearchParam<AppCategoryVo> params, HttpServletResponse response) {
        List<AppCategoryVo> list = appCategoryService.findVo(params.getEntity());
        appCategoryService.downLoad(response, list);
    }

    public void upload(MultipartFile file) {
        try {
            appCategoryService.upload(file.getInputStream());
        } catch (IOException e) {
            throw new RRException("上传的文件有误!");
        }
    }


    @RequestMapping("/info/{id}")
    @RequiresPermissions("app:category:info")
    public R info(@PathVariable("id") Long id) {
        AppCategoryVo vo = appCategoryService.getVo(id);

        return R.ok(vo);
    }

    @RequestMapping("/save")
    @RequiresPermissions("app:category:save")
    public R save(@RequestBody AppCategoryVo vo) {
        appCategoryService.saveVo(vo);
        return R.ok(vo);
    }

    @RequestMapping("/update")
    @RequiresPermissions("app:category:update")
    public R update(@RequestBody AppCategoryVo vo) {
        appCategoryService.updateVo(vo);

        return R.ok(vo);
    }

    @RequestMapping("/delete")
    @RequiresPermissions("app:category:delete")
    public R delete(Long[] ids) {
        appCategoryService.deleteBatch(ids);

        return R.ok();
    }

    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order != null ? order : "id desc");
        List<AppCategoryVo> list = appCategoryService.findVo(null);
        return R.ok(list);
    }



    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String, Object> options = new HashMap();
        //找顶层的
        List<AppCategoryEntity> list = appCategoryService.findByLevel(0);
        options.put("topLevelOptions", list);
        return R.ok(options);
    }


    /***********自定义开始***********/


    @RequestMapping("/list/tree")
    @RequiresPermissions("app:category:list")
    public R listTree(@RequestBody SearchParam<AppCategoryVo> params) {
        List<AppCategoryVo> list = appCategoryService.findVo(params.getEntity());
        AppCategoryEntity entity = params.getEntity().getEntity();
        if(!StringUtil.isBlank(entity.getName())){ //查询直接返回
            return R.ok(list);
        }
        List<AppCategoryVo> treeList;
        treeList = list.stream().filter(item->{
            item.children = list.stream().filter(it->it.getAppCategory().getParentType().equals(item.getAppCategory().getType())).collect(Collectors.toList());
           return item.getAppCategory().getLevel() == 0;
        }).collect(Collectors.toList());
        return R.ok(treeList);
    }
}
